﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.DeclareWarLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class DeclareWarLogEntry : LogEntry, IEncyclopediaLog, IChatNotification, IWarLog
  {
    [SaveableField(190)]
    public readonly IFaction Faction1;
    [SaveableField(191)]
    public readonly IFaction Faction2;
    [SaveableField(192)]
    public readonly Hero Faction1Leader;

    internal static void AutoGeneratedStaticCollectObjectsDeclareWarLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Faction1);
      collectedObjects.Add((object) this.Faction2);
      collectedObjects.Add((object) this.Faction1Leader);
    }

    internal static object AutoGeneratedGetMemberValueFaction1(object o)
    {
      return (object) ((DeclareWarLogEntry) o).Faction1;
    }

    internal static object AutoGeneratedGetMemberValueFaction2(object o)
    {
      return (object) ((DeclareWarLogEntry) o).Faction2;
    }

    internal static object AutoGeneratedGetMemberValueFaction1Leader(object o)
    {
      return (object) ((DeclareWarLogEntry) o).Faction1Leader;
    }

    public bool IsVisibleNotification => true;

    public override ChatNotificationType NotificationType
    {
      get => this.AdversityNotification(this.Faction1, this.Faction2);
    }

    public DeclareWarLogEntry(IFaction faction1, IFaction faction2)
    {
      this.Faction1 = faction1;
      this.Faction2 = faction2;
      this.Faction1Leader = faction1.Leader;
    }

    public bool IsRelatedToWar(StanceLink stance, out IFaction effector, out IFaction effected)
    {
      IFaction faction1 = stance.Faction1;
      IFaction faction2 = stance.Faction2;
      effector = faction1;
      effected = faction2;
      if (faction1 == this.Faction1 && faction2 == this.Faction2)
        return true;
      return faction1 == this.Faction2 && faction2 == this.Faction1;
    }

    public TextObject GetNotificationText()
    {
      Hero hero = this.Faction1Leader ?? this.Faction1.Leader;
      TextObject text;
      if (hero != null)
      {
        text = GameTexts.FindText("str_factions_declare_war_news");
        text.SetTextVariable("RULER_NAME", hero.Name);
      }
      else
        text = GameTexts.FindText("str_factions_declare_war_news_direct");
      text.SetTextVariable("FACTION1_NAME", this.Faction1.EncyclopediaLinkWithName);
      text.SetTextVariable("FACTION2_NAME", this.Faction2.EncyclopediaLinkWithName);
      return text;
    }

    public override void GetConversationScoreAndComment(
      Hero talkTroop,
      bool findString,
      out string comment,
      out ImportanceEnum score)
    {
      score = ImportanceEnum.Zero;
      comment = "";
      if (this.Faction1.IsEliminated || !this.Faction1.Leader.Clan.IsRebelClan || talkTroop.Clan != this.Faction1)
        return;
      score = ImportanceEnum.MatterOfLifeAndDeath;
      if (!findString)
        return;
      comment = "str_comment_we_have_rebelled";
    }

    public override int GetAsRumor(Settlement talkSettlement, ref TextObject comment)
    {
      int asRumor = 0;
      if (this.Faction1 == talkSettlement.MapFaction)
      {
        comment = new TextObject("{=mrmxEklL}So looks like it's war with {ENEMY_NAME}. Well, I don't deny they deserve it, but it will fall hardest on the poor folk like us.");
        comment.SetTextVariable("ENEMY_NAME", FactionHelper.GetTermUsedByOtherFaction(this.Faction2, talkSettlement.MapFaction, false));
        return 10;
      }
      if (this.Faction2 != talkSettlement.MapFaction)
        return asRumor;
      comment = new TextObject("{=SVebFiHQ}So looks like {ENEMY_NAME} want war with us. Well, I say we show the bastards who we are!");
      comment.SetTextVariable("ENEMY_NAME", FactionHelper.GetTermUsedByOtherFaction(this.Faction1, talkSettlement.MapFaction, false));
      return 10;
    }

    public override string ToString() => this.GetEncyclopediaText().ToString();

    public bool IsVisibleInEncyclopediaPageOf<T>(T obj) where T : MBObjectBase
    {
      return (object) obj == this.Faction1 || (object) obj == this.Faction2;
    }

    public TextObject GetEncyclopediaText() => this.GetNotificationText();
  }
}
